var curr = "1";
var pageSize = "10";
var table;
$(function () {
    //获取表格数据
    getAllFilesList(0, 10);

})

/**
 * 后台返回数据填充表格 demo
 */
function getAllFilesList(currt, size) {
    var obsFile = {};
    obsFile.userId = sessionStorage.getItem("id");
    var page = {};
    page.currt = currt;
    page.size = size;
    var obj = {};
    obj.obsFile = obsFile;
    obj.page = page;
    $.ajax({
        url: sessionStorage.getItem("rootPath") + "/files/selectDocumentList",
        method: "post",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(obj),
        success: function (res) {
            //登录失效，跳转到登录页面
            if(res.status == -1){
                window.top.location.href = 'login.html';
            }
            renderTable(res);
            renderpage(res);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log("失败" + XMLHttpRequest.status + ":" + textStatus + ":" + errorThrown);
        }
    })
}

/**
 * 表格渲染
 */
function renderTable(data) {
    layui.use('table', function () {
        table = layui.table;
        table.render({
            elem: '#allFilesTable'
            , url: '../../admin/data/common.json'
            , page: false
            , height: 490
            , toolbar: '#toolbar' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: []
            , parseData: function (res) {
                return {
                    "code": 0,
                    "msg": "",
                    "data": data.data
                }
            }
            , cols: [[
                {type: 'checkbox', width: 60},
                {
                    field: 'type', width: 80, title: '图标', templet: function (data) {
                        if (data.type == "Word") {
                            return '<img src="admin/images/tag_word.png" style="width:30px;"/>';
                        } else if (data.type == "Pdf") {
                            return '<img src="admin/images/tag_pdf.png" style="width:30px;"/>';
                        } else if (data.type == "Excel") {
                            return '<img src="admin/images/tag_excel.png" style="width:30px;"/>';
                        } else if (data.type == "Ppt") {
                            return '<img src="admin/images/tag_ppt.png" style="width:30px;"/>';
                        }
                    }
                }
                , {
                    field: 'fileName', title: '文件名', sort: true, templet: function (data) {
                        return "<a href='javascript:void(0)' style='color: #1aa5fb'" + " onclick=openFile('" + data.path + "','" + data.type + "')>" + data.fileName + "</a>";
                    }
                }
                , {field: 'size', title: '文件大小', sort: true}
                , {field: 'makeTime', title: '上传时间', sort: true}
                , {
                    fixed: 'right', title: '操作', templet: function (data) {
                        var btns = "";
                        btns += ' <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a>';
                        btns += ' <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="preview"><i class="layui-icon layui-icon-refresh"></i>重命名</a>';
                        return btns;
                    }
                }
            ]]
        });

        //监听行工具事件
        table.on('tool(allFilesDemo)', function (obj) {
            var data = obj.data;
            if (obj.event === 'delete') {
                layer.confirm('真的删除该文件吗？', function (index) {
                    recycleFile(data.fileId, data.path, data.type);
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === 'preview') {
                var data = obj.data;
                layer.prompt({title: '输入新名称', formType: 0, value: data.fileName}, function (fileName, index) {
                    var obsFile = {};
                    obsFile.userId = sessionStorage.getItem("id");
                    obsFile.fileName = data.fileName;
                    obsFile.fileId = data.fileId;
                    obsFile.path = data.path.substring(0, data.path.length - data.fileName.length) + fileName;
                    $.ajax({
                        url: sessionStorage.getItem("rootPath") + "/files/rename",
                        data: JSON.stringify(obsFile),
                        dataType: 'json',
                        contentType: 'application/json;charset=utf-8',
                        type: 'post',
                        success: function (res) {
                            layer.msg("重命名成功！", {icon: 1, time: 1000}, function () {
                                getAllFilesList();
                                layer.close(index);
                            });
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            layer.msg("重命名失败！", {icon: 2, time: 1000});
                            console.log("失败" + XMLHttpRequest.status + ":" + textStatus + ":" + errorThrown);
                        }
                    })
                });
            }
        });
    });
}

/**
 * 删除文件
 */
function recycleFile(id, path, type) {
    var obsFile = {};
    obsFile.fileId = id;
    obsFile.userId = sessionStorage.getItem("id");
    obsFile.path = path;
    obsFile.type = type;
    obsFile.fileState=0;
    $.ajax({
        url: sessionStorage.getItem("rootPath") + "/files/deleteFile",
        data: JSON.stringify(obsFile),
        dataType: 'json',
        contentType: 'application/json;charset=utf-8',
        type: 'post',
        success: function (res) {
            layer.msg("删除成功！", {icon: 1, time: 1000});
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            layer.msg("删除失败！", {icon: 2, time: 1000});
            console.log("失败" + XMLHttpRequest.status + ":" + textStatus + ":" + errorThrown);
        }
    })
}

/**
 * 分页
 */
function renderpage(date) {
    layui.use('laypage', function () {
        var laypage = layui.laypage;

        //执行一个laypage实例
        laypage.render({
            elem: 'allFilesTable_page' //注意，这里的 test1 是 ID，不用加 # 号
            , layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']
            , count: date.total //数据总数，从服务端得到
            , limits: [10, 20, 30]
            , curr: curr
            , limit: pageSize
            , jump: function (obj, first) {
                //首次不执行
                if (!first) {
                    page = obj.curr;
                    pageSize = obj.limit;
                    getAllFilesList(curr - 1, pageSize);
                }
            }
        });

    });
}

/**
 * 下载文件
 */
function ObsDownload() {
    var checkStatus = table.checkStatus("allFilesTable");
    if (checkStatus.data.length > 0) {
        //遍历下载
        for (var i = 0; i < checkStatus.data.length; i++) {
            var res = "https://didacloudpan.obs.cn-north-4.myhuaweicloud.com/" + checkStatus.data[i].path + '?response-content-disposition=attachment';
            window.open(res, "_blank");
        }
    } else {
        layui.use('layer', function () { //独立版的layer无需执行这一句
            var $ = layui.jquery, layer = layui.layer; //独立版的layer无需执行这一句
            layer.alert('请至少选择一张图片');
        })
    }
}

/**
 * 打开文件
 */
function openFile(path, type) {
    if(type=="Word" || type=="Excel" || type=="Ppt"){
	    var res = "https://didacloudpan.obs.cn-north-4.myhuaweicloud.com/"+path+'?response-content-disposition=attachment';
	    window.open('https://view.officeapps.live.com/op/view.aspx?src='+res,"_blank")
     }else if(type=="Pdf"){
	     window.open("https://didacloudpan.obs.cn-north-4.myhuaweicloud.com/" + path, "_blank");
     }else{
        layui.use('layer', function () { //独立版的layer无需执行这一句
            var $ = layui.jquery, layer = layui.layer; //独立版的layer无需执行这一句
            layer.msg("该类型暂时不支持预览");
        })
    }
}
